package com.action;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.bean.ResponseBean;
import com.excel.ExcelUtil;
import com.pojo.Business;
import com.service.BusinessStatisticsService;
import com.service.StaffService;
import com.service.TrailService;
import com.util.ValidateHelper;
import com.vo.BusinessStatisticsVo;
import com.vo.StaffVO;
import com.vo.TrailVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.InputStreamResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.text.ParseException;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/trail")
public class TrailAction {
    @Autowired
    private TrailService trailService;

    @Autowired
    private BusinessStatisticsService businessStatisticsService;

    @Autowired
    private StaffService staffService;

    @Value("${upload.dir}")
    private String download ;

    /**
     * 查询轨迹
     */
    @RequestMapping("/selectTrail")
    public ResponseBean selectTrail(@RequestParam(required = false) String businessName, @RequestParam(required = false) String staffName,
                                    @RequestParam(required = false) String start_time, @RequestParam(required = false) String end_time,
                                    @RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "10") int limit,
                                    HttpServletRequest request){
        String queryStr = null;
        IPage iPage = trailService.getTrailList(businessName, staffName, start_time, end_time, page, limit, queryStr);
        return new ResponseBean(0,iPage.getTotal(),"成功",iPage.getRecords());
    }

    /**
     * 轨迹导出Excel
     */
    @RequestMapping("/exportByTrail")
    public ResponseEntity<InputStreamResource> exportByTrail(@RequestParam(required = false) String business_name, @RequestParam(required = false) String staffName,
                                                             @RequestParam(required = false) String start_time, @RequestParam(required = false) String end_time,
                                                             HttpServletRequest request) throws IOException {
        String queryStr = null;
        List<TrailVo> trailVoList =  trailService.exportTrailList(business_name,staffName,start_time,end_time, queryStr);
        String cnFileName = new String("员工轨迹记录".getBytes(), "utf-8");
        String name1 = cnFileName + new Date().getTime() + ".xls" ;
        String fileName = download + "/excel/" + name1 ;
        if(!new File(download + "/excel/").exists()) {
            new File(download + "/excel/").mkdirs() ;
        }
        try {
            trailService.exportByTrail(fileName,"员工轨迹记录",trailVoList);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        FileSystemResource file = new FileSystemResource (fileName) ;
        HttpHeaders headers = new HttpHeaders();
        headers.add("Cache-Control", "no-cache, no-store, must-revalidate");
        headers.add("Content-Disposition", String.format("attachment; filename=\"%s\"", new String(name1.getBytes(), "iso-8859-1")));
        headers.add("Pragma", "no-cache");
        headers.add("Expires", "0");

        return ResponseEntity
                .ok()
                .headers(headers)
                .contentLength(file.contentLength())
                .contentType(MediaType.parseMediaType("application/octet-stream"))
                .body(new InputStreamResource(file.getInputStream()));
    }

    @RequestMapping(value = "/trailDetail")
    public ResponseBean trailDetail (String staffBh, String start_time) {
        List<TrailVo> trailVoList = trailService.getTrailListByStaff(staffBh, start_time);
        return new ResponseBean(200,"成功", trailVoList);
    }

    @RequestMapping(value = "/getTrailByBusiBh")
    public ResponseBean getTrailByBusiBh (Integer busiBh, String start_time,
                                          @RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "10") int limit) {
        IPage<TrailVo> iPage = trailService.getTrailListByBusiness(busiBh, start_time, page, limit);
        return new ResponseBean(0,iPage.getTotal(),"成功",iPage.getRecords());
    }

    @RequestMapping(value = "/selectTrailByBusiness")
    public ResponseBean selectTrailByBusiness (@RequestParam(required = false) String staffName, HttpServletRequest request,
                                               @RequestParam(required = false) String start_time, @RequestParam(required = false) String end_time,
                                               @RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "10") int limit) {
        String queryStr = null;
        IPage<TrailVo> iPage = trailService.getTrailList("", staffName, start_time, end_time, page, limit, queryStr);
        return new ResponseBean(0,iPage.getTotal(),"成功",iPage.getRecords());
    }

    /**
     * 获取物业每天访问详情
     * @return
     */
    @RequestMapping(value = "/getWyStatisticsDetail")
    public ResponseBean getWyStatisticsDetail (String wyId, String queryDate,
                                               @RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "10") int limit) throws ParseException {
        IPage<BusinessStatisticsVo> iPage = businessStatisticsService.getWyStatisticsDetail(wyId, queryDate, page, limit);
        return new ResponseBean(0,iPage.getTotal(),"成功",iPage.getRecords());
    }

    /**
     * 获取商户每天访问详情
     * @return
     */
    @RequestMapping(value = "/getBusinessStatisticsDetail")
    public ResponseBean getBusinessStatisticsDetail (String businessBh, String queryDate,
                                                     @RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "10") int limit) throws ParseException {
        IPage<TrailVo> iPage = trailService.getBusinessStatisticsDetail(businessBh, queryDate, page, limit);
        return new ResponseBean(0,iPage.getTotal(),"成功",iPage.getRecords());
    }



    /**
     * 获取员工每天访问详情
     * @return
     */
    @RequestMapping(value = "/getStaffStatisticsDetail")
    public ResponseBean getStaffStatisticsDetail (String staffBh, String queryDate,
                                                  @RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "10") int limit) throws ParseException {
        IPage<TrailVo> iPage = trailService.getStaffStatisticsDetail(staffBh, queryDate, page, limit);
        return new ResponseBean(0,iPage.getTotal(),"成功",iPage.getRecords());
    }

    /**
     * 获取员工未活动记录
     * @return
     */
    @RequestMapping(value = "/getStaffTrail")
    public ResponseBean getStaffTrail (String name, @RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "10") int limit ,
                                       @RequestParam(required = false) String wuye , @RequestParam(required = false) String qiye, @RequestParam(required = false) String days) throws ParseException {
        IPage<StaffVO> iPage = staffService.getStaffTrail(page, limit, name, wuye, qiye, days);
        return new ResponseBean(0,iPage.getTotal(),"成功",iPage.getRecords());
    }

    /**
     * 商户访问量导出Excel
     */
    @RequestMapping("/getStaffTrailByExport")
    public ResponseEntity<InputStreamResource> getStaffTrailByExport(String name, String wuye , String qiye, String days) throws IOException {
        List<StaffVO> staffVOList =  staffService.getStaffTrailByExport(name,wuye,qiye,days);
        String cnFileName = new String("员工未活动时间统计".getBytes(), "utf-8");
        String name1 = cnFileName + new Date().getTime() + ".xls" ;
        String fileName = download + "/excel/" + name1 ;
        if(!new File(download + "/excel/").exists()) {
            new File(download + "/excel/").mkdirs() ;
        }
        try {
            ExcelUtil.exportExcelByStaffTrail(fileName,"员工未活动时间统计",staffVOList);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        FileSystemResource file = new FileSystemResource (fileName) ;
        HttpHeaders headers = new HttpHeaders();
        headers.add("Cache-Control", "no-cache, no-store, must-revalidate");
        headers.add("Content-Disposition", String.format("attachment; filename=\"%s\"", new String(name1.getBytes(), "iso-8859-1")));
        headers.add("Pragma", "no-cache");
        headers.add("Expires", "0");

        return ResponseEntity
                .ok()
                .headers(headers)
                .contentLength(file.contentLength())
                .contentType(MediaType.parseMediaType("application/octet-stream"))
                .body(new InputStreamResource(file.getInputStream()));
    }


}
